package com.example.studentknowledgesys.dao;

import com.example.studentknowledgesys.entity.PointRule;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 光的代言人
 */
@Mapper
public interface PointRuleMapper {
    // 插入积分规则
    @Insert("INSERT INTO point_rules(rule_name, rule_type, point_value, description, is_active) " +
            "VALUES(#{ruleName}, #{ruleType}, #{pointValue}, #{description}, #{isActive})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertPointRule(PointRule pointRule);

    // 更新积分规则
    @Update("UPDATE point_rules SET rule_name = #{ruleName}, rule_type = #{ruleType}, " +
            "point_value = #{pointValue}, description = #{description}, " +
            "is_active = #{isActive}, updated_at = NOW() WHERE id = #{id}")
    int updatePointRule(PointRule pointRule);

    // 删除积分规则
    @Delete("DELETE FROM point_rules WHERE id = #{id}")
    int deletePointRule(Integer id);

    // 根据ID查询积分规则
    @Select("SELECT * FROM point_rules WHERE id = #{id}")
    PointRule selectPointRuleById(Integer id);

    // 查询所有积分规则
    @Select("SELECT * FROM point_rules")
    List<PointRule> selectAllPointRules();

    // 查询启用状态的积分规则
    @Select("SELECT * FROM point_rules WHERE is_active = true")
    List<PointRule> selectActivePointRules();

    // 根据规则类型查询启用状态的规则
    @Select("SELECT * FROM point_rules WHERE rule_type = #{ruleType} AND is_active = true")
    PointRule selectActiveRuleByType(String ruleType);
}
